package com.outsourcing.bottle.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.IBinder;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ext.SatelliteMenu;
import android.view.ext.SatelliteMenu.SateliteClickedListener;
import android.view.ext.SatelliteMenuItem;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.db.LoginUserInfoTable;
import com.outsourcing.bottle.db.PushNoticeInfoTable;
import com.outsourcing.bottle.domain.LoginUserInfoEntry;
import com.outsourcing.bottle.domain.PushNoticeInfo;
import com.outsourcing.bottle.ui.fragment.BottleFragment;
import com.outsourcing.bottle.ui.fragment.ExchangeFragment;
import com.outsourcing.bottle.ui.fragment.FriendFragment;
import com.outsourcing.bottle.util.ActivityManager;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.BottleService;
import com.outsourcing.bottle.util.ImageLoader;
import com.outsourcing.bottle.widget.ViewPagerIndicator;

public class HomeActivity extends BasicActivity implements Callback, OnClickListener {

	private final int user_baseinfo_notfull = 0;
	private final int complete_info = 1;
	
	private final int menu_uploadphoto = 5;
	private final int menu_exprofile = 4;
	private final int menu_exphoto = 3;
	private final int menu_gainbt = 2;
	private final int menu_sendbt = 1;

	private ImageView backView;
	private ImageButton menuView;
	public TextView titleView;
	private ImageView spaceView;

	public PagerAdapter mPagerAdapter;
	public ViewPager  mViewPager;
	public ViewPagerIndicator mIndicator;
	private Handler handler;
	public BottleFragment bottleFragment;
	public ExchangeFragment exchangeFragment;
	public FriendFragment friendFragment;
	
	private LinearLayout bottomLayout;
	private ImageView avatarView;
	private TextView contentView;
	private TextView btnCount;
//	private ImageView messageView;
	private ImageLoader userImageLoader;
	private int newNoticeCount;
	private int newMessageCount;
	private int newBottleFeedCount;
	private int newExchangeFeedCount;
	private int newMaybeKownCount;
	private String message_content;
	private String message_avatar;
	
	SatelliteMenu menu;
	
	public int exchange_item;
	public int bottle_item;
	public int friend_item;
	
	public String[] exchange_choices;
	public String[] bottle_choices;
	public String[] friend_choices;
	
	private ServiceConnection conn;
	private PushBroadcastReceiver receiver;
	private PushNoticeInfo pushInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		handler = new Handler(this);
		
		userImageLoader = new ImageLoader(this, AppContext.UserAvatarIcon);
		setContentView(R.layout.home_pager);
		if (!AppContext.FIRTH_INIT) {
			AppContext.getInstance().setCurrentItem(1);
		}
		conn = new ServiceConnection() {

			@Override
			public void onServiceDisconnected(ComponentName name) {

			}

			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {
				
			}
		};
		Intent intent = new Intent(this, BottleService.class);
		startService(intent);
		bindService(intent, conn, Context.BIND_AUTO_CREATE);
		
		initLayout();
		receiver = new PushBroadcastReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction("push");
		registerReceiver(receiver, filter);
		
		//网络定位
		networkLocat();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		int currentItem = 0;
		currentItem = AppContext.getInstance().getCurrentItem();
		boolean needRefresh = getIntent().getBooleanExtra("need_refresh", false);
		if (needRefresh) {
			switch (currentItem) {
			case 0:
				exchangeFragment.init = false;
				break;
			case 1:
				bottleFragment.init = false;
				break;
			case 2:
				friendFragment.init = false;
				break;
			default:
				break;
			}
		}
		mViewPager.setCurrentItem(currentItem);
		AppContext.FIRTH_INIT = true;
		try {
			pushNoticeUpdateUI();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initLayout() {
		mPagerAdapter = new PagerAdapter(getSupportFragmentManager(), this);
		mPagerAdapter.addFragment(getString(R.string.public_txt_exchange), ExchangeFragment.class, null);
		mPagerAdapter.addFragment(getString(R.string.public_txt_bottle), BottleFragment.class, null);
		mPagerAdapter.addFragment(getString(R.string.public_txt_btfriend), FriendFragment.class, null);
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setOffscreenPageLimit(0);
		mViewPager.setAdapter(mPagerAdapter);
		mIndicator = (ViewPagerIndicator) findViewById(R.id.indicator);
		mViewPager.setOnPageChangeListener(mIndicator);
		mIndicator.setContext(this);
		mIndicator.init(AppContext.getInstance().getCurrentItem(), mPagerAdapter.getCount(), mPagerAdapter);
		Resources res = getResources();
		Drawable prev = res.getDrawable(R.drawable.arrow_left);
		Drawable next = res.getDrawable(R.drawable.arrow_right);
		mIndicator.setTextColor(Color.WHITE);
		mIndicator.setArrows(prev, next);
		mIndicator.setOnClickListener(new OnIndicatorClickListener());
		
		if (menu == null) {
			LinearLayout layout = (LinearLayout) findViewById(R.id.menu_layout);
			menu = new SatelliteMenu(this);
//			menu.setExpandDuration(500);
			if (CanvasWidth >= 480) {
				menu.setSatelliteDistance(200);
			} else {
				menu.setSatelliteDistance(100);
			}
			menu.setTotalSpacingDegree(90.0f);
			menu.setCloseItemsOnClick(true);
			layout.addView(menu);
			
			List<SatelliteMenuItem> items = new ArrayList<SatelliteMenuItem>();
			items.add(new SatelliteMenuItem(menu_exprofile, R.drawable.menu_exprofile));
			items.add(new SatelliteMenuItem(menu_exphoto, R.drawable.menu_exphoto));
			items.add(new SatelliteMenuItem(menu_gainbt, R.drawable.menu_gainbt));
			items.add(new SatelliteMenuItem(menu_sendbt, R.drawable.menu_sendbt));
			items.add(new SatelliteMenuItem(menu_uploadphoto, R.drawable.menu_uploadphoto));
			menu.addItems(items);
		}
		menu.setOnItemClickedListener(new SateliteClickedListener() {

			public void eventOccured(int id) {
				switch (id) {
				case menu_sendbt: //扔瓶子
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent throwIntent = new Intent(HomeActivity.this, ChooseBottleActivity.class);
								throwIntent.putExtra("type", "throw");
								startActivity(throwIntent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_gainbt: //捞瓶子
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent tryIntent = new Intent(HomeActivity.this, ChooseBottleActivity.class);
								tryIntent.putExtra("type", "try");
								startActivity(tryIntent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_exphoto:  //交换照片
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent exchangeInfoIntent = new Intent(HomeActivity.this, ChooseFriendActivity.class);
								exchangeInfoIntent.putExtra("choose_friend_type", AppContext.CHOOSE_FRIEND_EXCHANGE_PHOTO_TYPE);
								startActivity(exchangeInfoIntent);
								AppContext.getInstance().setFromExchangeTimeToChangeFriend(true);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_exprofile: //交换资料
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								LoginUserInfoTable userTable = new LoginUserInfoTable();
								LoginUserInfoEntry userEntry = userTable.getLoginUserInfo(AppContext.getInstance().getLogin_uid());
								if (userEntry.getBasicinfo_fullfill() == 0) {
									handler.sendEmptyMessage(user_baseinfo_notfull);
								} else {
									Intent exchangeFileIntent = new Intent(HomeActivity.this, ChooseFriendActivity.class);
									exchangeFileIntent.putExtra("choose_friend_type", AppContext.CHOOSE_FRIEND_EXCHANGE_INFO_TYPE);
									startActivity(exchangeFileIntent);
									AppContext.getInstance().setFromExchangeTimeToChangeFriend(true);
								}
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_uploadphoto:
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent intent = new Intent(HomeActivity.this, ExpandEditActivity.class);
								Bundle extras = new Bundle();
								extras.putString("from_type", AppContext.UPLOAD_PHOTO);
								extras.putInt("albumid", 0);
								extras.putInt("ouid", AppContext.getInstance().getLogin_uid());
								intent.putExtras(extras);
								startActivity(intent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				default:
					break;
				}
			}
		});

		backView = (ImageView) findViewById(R.id.topbar_back);
		backView.setOnClickListener(this);
		menuView = (ImageButton) findViewById(R.id.topbar_menu);
		menuView.setOnClickListener(this);
		titleView = (TextView) findViewById(R.id.topbar_title);
		spaceView = (ImageView) findViewById(R.id.topbar_space);
		spaceView.setOnClickListener(this);
		
		bottomLayout = (LinearLayout) findViewById(R.id.notice_bottom_layout);
		bottomLayout.setOnClickListener(this);
		avatarView = (ImageView) findViewById(R.id.notice_avatar);
		contentView = (TextView) findViewById(R.id.notice_text);
		btnCount = (TextView) findViewById(R.id.notice_number);
//		messageView = (ImageView) findViewById(R.id.notice_message_count);
//		messageView.setOnClickListener(this);
	}
	
	@Override
	public void sysMesPositiveButtonEvent(int what) {
		if (what == complete_info) {
			Intent intent = new Intent(this, SettingActivity.class);
			startActivity(intent);
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case user_baseinfo_notfull:
			LoginUserInfoTable userTable = new LoginUserInfoTable();
			LoginUserInfoEntry userEntry = userTable.getLoginUserInfo(AppContext.getInstance().getLogin_uid());
			showChoseMes(userEntry.getBasicinfo_tip(), complete_info);
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		if (v == backView) {
			if (mIndicator.mCurItem == 0) {
				exchangeFragment.doGetExTimeline_Refresh();
			} else if (mIndicator.mCurItem == 1) {
				bottleFragment.doGetBottleTimeline_Refresh();
			} else if (mIndicator.mCurItem == 2) {
				friendFragment.doGetBTfriendList_Refresh();
			}
		} else if (v == menuView) {
			Intent intent = new Intent(this, MoreActivity.class);
			startActivity(intent);
		} 
//		else if (v == messageView) {
//			doMessageAction();
//		} 
		else if (v == bottomLayout) {
			doFeedsAction();
		} else if (v == spaceView) {
			Intent intent = new Intent(this, UserSpaceActivity.class);
			intent.putExtra("ouid", AppContext.getInstance().getLogin_uid());
			startActivity(intent);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
//			moveTaskToBack(true);
			showExitTips();
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_HOME) {
			moveTaskToBack(true);
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}
	
	public void showExitTips() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(getString(R.string.logout_home));
		builder.setPositiveButton(getString(R.string.confirm_ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						doExit();
						ActivityManager.finishAll();
						android.os.Process.killProcess(android.os.Process.myPid());
						System.exit(0);
						dialog.dismiss();
//						moveTaskToBack(true);
					}
				});
		builder.setNegativeButton(getString(R.string.confirm_cancel),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.show();
	}
	
	public void doExit() {
		NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		nm.cancel(R.string.app_name);
		nm.cancel(R.drawable.logo + 1);
		stopService(new Intent(this, BottleService.class));
	}


	public void doMessageAction() {
		
		final Context dialogContext = new ContextThemeWrapper(this,
				android.R.style.Theme_Light);
		String[] choices = new String[2];
		if (newNoticeCount > 0) {
			choices[0] = newNoticeCount + getString(R.string.bottle_txt_notice_list);
		} else {
			choices[0] = getString(R.string.bottle_txt_notice_list);
		}
		if (newMessageCount > 0) {
			choices[1] = newMessageCount + getString(R.string.bottle_txt_message_list);
		} else {
			choices[1] = getString(R.string.bottle_txt_message_list);
		}
	
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, choices);

		final AlertDialog.Builder builder = new AlertDialog.Builder(
				dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (which == 0) {
							Intent intent = new Intent(HomeActivity.this, NoticeListActivity.class);
							startActivity(intent);
						} else {
							Intent intent = new Intent(HomeActivity.this, MessageListActivity.class);
							startActivity(intent);
						}
					}
				});
		builder.create().show();
	}
	
	public void doFeedsAction() {
		final Context dialogContext = new ContextThemeWrapper(this, android.R.style.Theme_Light);
		final List<String> choices = new ArrayList<String>(); 
		if (newBottleFeedCount > 0) {
			choices.add(newBottleFeedCount + getString(R.string.bottle_txt_new_bottle_feed));
		}
		if (newExchangeFeedCount > 0) {
			choices.add(newExchangeFeedCount + getString(R.string.bottle_txt_new_exchange_feed));
		}
		if (newNoticeCount > 0) {
			choices.add(newNoticeCount + getString(R.string.bottle_txt_notice_list));
		}
		if (newMessageCount > 0) {
			choices.add(newMessageCount + getString(R.string.bottle_txt_message_list));
		}
		if (newMaybeKownCount > 0) {
			choices.add(newMaybeKownCount + getString(R.string.bottle_txt_new_maybe_kown));
		}
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext, android.R.layout.simple_list_item_1, choices);
		final AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (choices.get(which).indexOf(getString(R.string.bottle_txt_new_bottle_feed)) != -1) {
							mViewPager.setCurrentItem(1);
							bottleFragment.doGetBottleTimeline_Refresh();
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_new_exchange_feed)) != -1) {
							mViewPager.setCurrentItem(0);
							exchangeFragment.doGetExTimeline_Refresh();
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_notice_list)) != -1) {
							Intent intent = new Intent(HomeActivity.this, NoticeListActivity.class);
							startActivity(intent);
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_message_list)) != -1) {
							Intent intent = new Intent(HomeActivity.this, MessageListActivity.class);
							startActivity(intent);
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_new_maybe_kown)) != -1) {
							AppContext.getInstance().setGotoMaybeKown(true);
							mViewPager.setCurrentItem(2);
						}
					}
				});
		builder.create().show();
	}
	

	/***
	 * 	最新的动态 :0
		最新的瓶子:1
		收到的瓶子:2
		扔出的瓶子:3
	 * @param mode
	 */
	public void doBottleMenuAction() {
		
		final Context dialogContext = new ContextThemeWrapper(this,
				android.R.style.Theme_Light);
		bottle_choices = this.getResources().getStringArray(R.array.bottle_menu_item);
	
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, bottle_choices);

		final AlertDialog.Builder builder = new AlertDialog.Builder(
				dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						bottle_item = which;
						titleView.setText(bottle_choices[which]);
						bottleFragment.doMenuItemSelect(which);
					}
				});
		builder.create().show();
	}
	
	/***
	 * 	最新的动态 :0
		最新的动态:1
		资料交换:2
		照片交换:3
	 * @param mode
	 */
	public void doExchangeMenuAction() {
		
		final Context dialogContext = new ContextThemeWrapper(this,
				android.R.style.Theme_Light);
		exchange_choices = this.getResources().getStringArray(R.array.exchange_menu_item);
	
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, exchange_choices);

		final AlertDialog.Builder builder = new AlertDialog.Builder(
				dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						exchange_item = which;
						titleView.setText(exchange_choices[which]);
						exchangeFragment.doMenuItemSelect(which);
					}
				});
		builder.create().show();
	}
	
	/***
	 * 	我的好友
	 *  可能认识的人
	 * @param mode
	 */
	public void doBTFriendMenuAction() {
		
		final Context dialogContext = new ContextThemeWrapper(this,
				android.R.style.Theme_Light);
		friend_choices = this.getResources().getStringArray(
				R.array.friend_menu_item);
	
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, friend_choices);

		final AlertDialog.Builder builder = new AlertDialog.Builder(
				dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						friend_item = which;
						titleView.setText(friend_choices[which]);
						friendFragment.doMenuItemSelect(which);
					}
				});
		builder.create().show();
	}
	
	public void doBTFriendMenuAction2() {
		
		final Context dialogContext = new ContextThemeWrapper(this,
				android.R.style.Theme_Light);
		friend_choices = this.getResources().getStringArray(
				R.array.friend_menu_item2);
	
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, friend_choices);

		final AlertDialog.Builder builder = new AlertDialog.Builder(
				dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						titleView.setText(friend_choices[which]);
						friend_item = which;
						friendFragment.doMenuItemSelect2(which);
					}
				});
		builder.create().show();
	}
	
	
	class OnIndicatorClickListener implements ViewPagerIndicator.OnClickListener {
		
		@Override
		public void onCurrentClicked(View v) {
			switch (mIndicator.mCurItem) {
			case 0:
				doExchangeMenuAction(); 
				break;
			case 1:
				doBottleMenuAction();
				break;
			case 2:
				if (friendFragment.getState() == FriendFragment.EXEU_GET_MYFRIEND_LIST_MORE
						|| friendFragment.getState() == FriendFragment.EXEU_GET_MYFRIEND_LIST) {
					friendFragment.doSelectGroupAction();
				} else if (friendFragment.getState() == FriendFragment.EXEU_GET_MAYBEKONW_LIST
						|| friendFragment.getState() == FriendFragment.EXEU_GET_MAYBEKONW_LIST_MORE){
					
					doBTFriendMenuAction2();
				} else {
					doBTFriendMenuAction();
				}
				break;
			default:
				break;
			}
		}

		@Override
		public void onNextClicked(View v) {
			mViewPager.setCurrentItem(Math.min(mPagerAdapter.getCount() - 1, mIndicator.getCurrentPosition() + 1));
		}

		@Override
		public void onPreviousClicked(View v) {
			mViewPager.setCurrentItem(Math.max(0, mIndicator.getCurrentPosition() - 1));
		}

	}
	
	class PagerAdapter extends FragmentPagerAdapter implements
			ViewPagerIndicator.PageInfoProvider {
		
		private Context mContext;
		private final ArrayList<FragmentInfo> fragments = new ArrayList<FragmentInfo>();
		private ArrayList<Fragment> mfragments = new ArrayList<Fragment>();
		
		protected final class FragmentInfo {
			private final String tag;
            @SuppressWarnings("unused")
			private final Class<?> clss;
            private final Bundle args;

            protected  FragmentInfo(String _tag, Class<?> _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }
		
		public PagerAdapter(FragmentManager fm, Context context) {
			super(fm);
			this.mContext = context;
		}
		
		public void addFragment(String tag, Class<?> clss, Bundle args) {
			FragmentInfo fragmentInfo = new FragmentInfo(tag, clss, args);
			fragments.add(fragmentInfo);
			Fragment fragment = Fragment.instantiate(mContext, clss.getName(), fragmentInfo.args);
			mfragments.add(fragment);
			if (clss.getName().equals(BottleFragment.class.getName())) {
				bottleFragment = (BottleFragment) fragment;
			} else if (clss.getName().equals(ExchangeFragment.class.getName())) {
				exchangeFragment = (ExchangeFragment) fragment;
			} else if (clss.getName().equals(FriendFragment.class.getName())) {
				friendFragment = (FriendFragment) fragment;
			}
		}
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
//			super.destroyItem(container, position, object);
		}
		
		
		@Override
		public Fragment getItem(int pos) {
			return mfragments.get(pos);
		}

		@Override
		public int getCount() {
			return fragments.size();
		}

		@Override
		public String getTitle(int pos) {
			return fragments.get(pos).tag;
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		try {
			if (conn != null) {
				unbindService(conn);
			}
			if (receiver != null) {
				unregisterReceiver(receiver);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void pushNoticeUpdateUI() {
		PushNoticeInfoTable table = new PushNoticeInfoTable();
		pushInfo = table.getPushNoticeInfo(AppContext.getInstance().getLogin_uid());
		if (pushInfo != null){
			newBottleFeedCount = pushInfo.getNew_btfeed_count();
			newExchangeFeedCount = pushInfo.getNew_exfeed_count();
			newMessageCount = pushInfo.getNew_message_count();
			newNoticeCount = pushInfo.getNew_notice_count();
			message_content = pushInfo.getMessage_content();
			message_avatar = pushInfo.getMessage_avatar();
			newMaybeKownCount = pushInfo.getNew_maybeknow_count();
		}
//		if (newMessageCount + newNoticeCount > 0) {
//			messageView.setImageResource(R.drawable.bottom_menu_notice_red);
//		} else {
//			messageView.setImageResource(R.drawable.bottom_menu_notice_yellow);
//		}
		if (newBottleFeedCount + newExchangeFeedCount + newMessageCount + newNoticeCount  + newMaybeKownCount > 0) {
			bottomLayout.setVisibility(View.VISIBLE);
			contentView.setText(message_content);
			btnCount.setText(newBottleFeedCount + newExchangeFeedCount + newMessageCount + newNoticeCount + newMaybeKownCount + "");
			userImageLoader.DisplayImage(message_avatar, avatarView, R.drawable.avatar_default_big);
		} else {
			bottomLayout.setVisibility(View.GONE);
			NotificationManager mNotifMan = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
			mNotifMan.cancelAll();
		}
	}
	
	public class PushBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bundle = intent.getExtras();
			if (bundle != null) {
				newBottleFeedCount = bundle.getInt("bottle_feed_count");
				newExchangeFeedCount = bundle.getInt("exchange_feed_count");
				newMessageCount = bundle.getInt("message_count");
				newNoticeCount = bundle.getInt("notice_count");
				newMaybeKownCount = bundle.getInt("newMaybeKownCount");
				message_content = bundle.getString("message_content");
				message_avatar = bundle.getString("message_avatar");
				newMaybeKownCount = bundle.getInt("maybe_kown_count");
				pushNoticeUpdateUI();
			}
		}
	}
	
}
